package com.example.qu_tao.View;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.qu_tao.R;
import com.example.qu_tao.entity.Address;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class BianjidizhiActivity extends AppCompatActivity {
    private TextView tvRegion;         // 地区显示文本
    private BottomSheetDialog regionDialog; // 地区选择对话框
    private Spinner spProvince, spCity, spDistrict; // 省市区选择器
    private Button btnConfirmRegion;     // 确认选择按钮
    private EditText etReceiver, etPhone, etDetailAddress; // 输入框
    private Switch switchDefault;        // 默认地址开关
    private Button saveButton,deleteButton; // 按钮
    private ImageView back;
    // 地区数据
    private List<String> provinces = new ArrayList<>();
    private List<List<String>> cities = new ArrayList<>();
    private List<List<List<String>>> districts = new ArrayList<>();
    private String selectedProvince = "";
    private String selectedCity = "";
    private String selectedDistrict = "";
    private Address editAddress;         // 编辑的地址对象
    // SharedPreferences配置
    private static final String PREFS_NAME = "AddressPrefs";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bianjidizhi);
        // 获取编辑状态和地址对象
        Intent intent = getIntent();
        boolean isEdit = intent.getBooleanExtra("isEdit", false);
        if (isEdit) {
            editAddress = intent.getParcelableExtra("address");
            // 确保编辑时使用原地址的ID（关键）
            if (editAddress.getId() == null) {
                editAddress.setId(UUID.randomUUID().toString());
            }
            setTitle("编辑收货地址");
            fillAddressInfo(editAddress);
        } else {
            setTitle("新增收货地址");
            editAddress = new Address(); // 使用带ID的构造函数
        }
        initViews();
        initRegionData();
        loadRegionDialog();
    }
    private void fillAddressInfo(Address address) {
        etReceiver = findViewById(R.id.e_receiver);
        etPhone = findViewById(R.id.e_phone);
        tvRegion = findViewById(R.id.tv_region);
        etDetailAddress = findViewById(R.id.e_detail_address);
        switchDefault = findViewById(R.id.switch_default);
        back = findViewById(R.id.iv_back);
        etReceiver.setText(address.receiver);
        etPhone.setText(address.phone);
        tvRegion.setText(address.region);
        etDetailAddress.setText(address.detailAddress);
        switchDefault.setChecked(address.isDefault());
        //返回
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 设置地区选择器
        String[] regionParts = address.region.split(" ");
        if (regionParts.length == 3) {
            String province = regionParts[0];
            String city = regionParts[1];
            String district = regionParts[2];
            int provinceIndex = provinces.indexOf(province);
            if (provinceIndex != -1) {
                spProvince.setSelection(provinceIndex);
                List<String> cityList = cities.get(provinceIndex);
                int cityIndex = cityList.indexOf(city);
                if (cityIndex != -1) {
                    spCity.setSelection(cityIndex);
                    List<String> districtList = districts.get(provinceIndex).get(cityIndex);
                    int districtIndex = districtList.indexOf(district);
                    if (districtIndex != -1) {
                        spDistrict.setSelection(districtIndex);
                    }
                }
            }
        }
    }
    private void initViews() {
        tvRegion = findViewById(R.id.tv_region);
        tvRegion.setOnClickListener(v -> {
            if (regionDialog != null && !regionDialog.isShowing()) {
                regionDialog.show();
            }
        });
        etReceiver = findViewById(R.id.e_receiver);
        etPhone = findViewById(R.id.e_phone);
        etDetailAddress = findViewById(R.id.e_detail_address);
        switchDefault = findViewById(R.id.switch_default);
        saveButton = findViewById(R.id.saveAddressButton);
        deleteButton = findViewById(R.id.deleteAddressButton);
        saveButton.setOnClickListener(v -> saveAddress());
        deleteButton.setOnClickListener(v -> deleteAddress());
    }

    private void initRegionData() {
        // 省份数据
        provinces.add("北京市");
        provinces.add("上海市");
        provinces.add("广东省");
        provinces.add("江苏省");
        provinces.add("浙江省");
        provinces.add("山东省");
        provinces.add("四川省");
        provinces.add("湖南省");
        provinces.add("湖北省");
        provinces.add("河南省");

        // 北京市下辖城市
        List<String> beijingCities = new ArrayList<>();
        beijingCities.add("北京市");
        cities.add(beijingCities);

        // 上海市下辖城市
        List<String> shanghaiCities = new ArrayList<>();
        shanghaiCities.add("上海市");
        cities.add(shanghaiCities);

        // 广东省下辖城市
        List<String> guangdongCities = new ArrayList<>();
        guangdongCities.add("广州市");
        guangdongCities.add("深圳市");
        guangdongCities.add("佛山市");
        guangdongCities.add("东莞市");
        guangdongCities.add("珠海市");
        cities.add(guangdongCities);

        // 江苏省下辖城市
        List<String> jiangsuCities = new ArrayList<>();
        jiangsuCities.add("南京市");
        jiangsuCities.add("苏州市");
        jiangsuCities.add("无锡市");
        jiangsuCities.add("常州市");
        jiangsuCities.add("扬州市");
        cities.add(jiangsuCities);

        // 浙江省下辖城市
        List<String> zhejiangCities = new ArrayList<>();
        zhejiangCities.add("杭州市");
        zhejiangCities.add("宁波市");
        zhejiangCities.add("温州市");
        zhejiangCities.add("嘉兴市");
        zhejiangCities.add("绍兴市");
        cities.add(zhejiangCities);

        // 山东省下辖城市
        List<String> shandongCities = new ArrayList<>();
        shandongCities.add("济南市");
        shandongCities.add("青岛市");
        shandongCities.add("烟台市");
        shandongCities.add("潍坊市");
        shandongCities.add("淄博市");
        cities.add(shandongCities);

        // 四川省下辖城市
        List<String> sichuanCities = new ArrayList<>();
        sichuanCities.add("成都市");
        sichuanCities.add("绵阳市");
        sichuanCities.add("德阳市");
        sichuanCities.add("南充市");
        sichuanCities.add("宜宾市");
        cities.add(sichuanCities);

        // 湖南省下辖城市
        List<String> hunanCities = new ArrayList<>();
        hunanCities.add("长沙市");
        hunanCities.add("株洲市");
        hunanCities.add("湘潭市");
        hunanCities.add("衡阳市");
        hunanCities.add("岳阳市");
        cities.add(hunanCities);

        // 湖北省下辖城市
        List<String> hubeiCities = new ArrayList<>();
        hubeiCities.add("武汉市");
        hubeiCities.add("黄石市");
        hubeiCities.add("十堰市");
        hubeiCities.add("宜昌市");
        hubeiCities.add("襄阳市");
        cities.add(hubeiCities);

        // 河南省下辖城市
        List<String> henanCities = new ArrayList<>();
        henanCities.add("郑州市");
        henanCities.add("开封市");
        henanCities.add("洛阳市");
        henanCities.add("平顶山市");
        henanCities.add("安阳市");
        cities.add(henanCities);

        // 北京市下辖区县
        List<List<String>> beijingDistricts = new ArrayList<>();
        List<String> beijingDistrict = new ArrayList<>();
        beijingDistrict.add("东城区");
        beijingDistrict.add("西城区");
        beijingDistrict.add("朝阳区");
        beijingDistrict.add("丰台区");
        beijingDistrict.add("石景山区");
        beijingDistricts.add(beijingDistrict);

        // 上海市下辖区县
        List<List<String>> shanghaiDistricts = new ArrayList<>();
        List<String> shanghaiDistrict = new ArrayList<>();
        shanghaiDistrict.add("黄浦区");
        shanghaiDistrict.add("徐汇区");
        shanghaiDistrict.add("长宁区");
        shanghaiDistrict.add("静安区");
        shanghaiDistrict.add("普陀区");
        shanghaiDistricts.add(shanghaiDistrict);

        // 广东省下辖区县
        List<List<String>> guangdongDistricts = new ArrayList<>();
        // 广州市下辖区县
        List<String> guangzhouDistrict = new ArrayList<>();
        guangzhouDistrict.add("越秀区");
        guangzhouDistrict.add("荔湾区");
        guangzhouDistrict.add("天河区");
        guangzhouDistrict.add("海珠区");
        guangzhouDistrict.add("白云区");
        guangdongDistricts.add(guangzhouDistrict);
        // 深圳市下辖区县
        List<String> shenzhenDistrict = new ArrayList<>();
        shenzhenDistrict.add("福田区");
        shenzhenDistrict.add("南山区");
        shenzhenDistrict.add("宝安区");
        shenzhenDistrict.add("龙岗区");
        shenzhenDistrict.add("盐田区");
        guangdongDistricts.add(shenzhenDistrict);
        // 佛山市下辖区县
        List<String> foshanDistrict = new ArrayList<>();
        foshanDistrict.add("禅城区");
        foshanDistrict.add("南海区");
        foshanDistrict.add("顺德区");
        foshanDistrict.add("三水区");
        foshanDistrict.add("高明区");
        guangdongDistricts.add(foshanDistrict);
        // 东莞市下辖区县（东莞市无下辖县级单位，这里简化处理）
        List<String> dongguanDistrict = new ArrayList<>();
        dongguanDistrict.add("莞城区");
        dongguanDistrict.add("南城区");
        dongguanDistrict.add("万江区");
        dongguanDistrict.add("东城区");
        guangdongDistricts.add(dongguanDistrict);
        // 珠海市下辖区县
        List<String> zhuhaiDistrict = new ArrayList<>();
        zhuhaiDistrict.add("香洲区");
        zhuhaiDistrict.add("斗门区");
        zhuhaiDistrict.add("金湾区");
        guangdongDistricts.add(zhuhaiDistrict);

        // 江苏省下辖区县
        List<List<String>> jiangsuDistricts = new ArrayList<>();
        // 南京市下辖区县
        List<String> nanjingDistrict = new ArrayList<>();
        nanjingDistrict.add("玄武区");
        nanjingDistrict.add("秦淮区");
        nanjingDistrict.add("建邺区");
        nanjingDistrict.add("鼓楼区");
        nanjingDistrict.add("浦口区");
        jiangsuDistricts.add(nanjingDistrict);
        // 苏州市下辖区县
        List<String> suzhouDistrict = new ArrayList<>();
        suzhouDistrict.add("姑苏区");
        suzhouDistrict.add("虎丘区");
        suzhouDistrict.add("吴中区");
        suzhouDistrict.add("相城区");
        suzhouDistrict.add("吴江区");
        jiangsuDistricts.add(suzhouDistrict);
        // 无锡市下辖区县
        List<String> wuxiDistrict = new ArrayList<>();
        wuxiDistrict.add("梁溪区");
        wuxiDistrict.add("锡山区");
        wuxiDistrict.add("惠山区");
        wuxiDistrict.add("滨湖区");
        wuxiDistrict.add("新吴区");
        jiangsuDistricts.add(wuxiDistrict);
        // 常州市下辖区县
        List<String> changzhouDistrict = new ArrayList<>();
        changzhouDistrict.add("天宁区");
        changzhouDistrict.add("钟楼区");
        changzhouDistrict.add("新北区");
        changzhouDistrict.add("武进区");
        changzhouDistrict.add("金坛区");
        jiangsuDistricts.add(changzhouDistrict);
        // 扬州市下辖区县
        List<String> yangzhouDistrict = new ArrayList<>();
        yangzhouDistrict.add("广陵区");
        yangzhouDistrict.add("邗江区");
        yangzhouDistrict.add("江都区");
        yangzhouDistrict.add("宝应县");
        yangzhouDistrict.add("仪征市");
        jiangsuDistricts.add(yangzhouDistrict);

        // 浙江省下辖区县
        List<List<String>> zhejiangDistricts = new ArrayList<>();
        // 杭州市下辖区县
        List<String> hangzhouDistrict = new ArrayList<>();
        hangzhouDistrict.add("上城区");
        hangzhouDistrict.add("拱墅区");
        hangzhouDistrict.add("西湖区");
        hangzhouDistrict.add("滨江区");
        hangzhouDistrict.add("萧山区");
        zhejiangDistricts.add(hangzhouDistrict);
        // 宁波市下辖区县
        List<String> ningboDistrict = new ArrayList<>();
        ningboDistrict.add("海曙区");
        ningboDistrict.add("江北区");
        ningboDistrict.add("北仑区");
        ningboDistrict.add("镇海区");
        ningboDistrict.add("鄞州区");
        zhejiangDistricts.add(ningboDistrict);
        // 温州市下辖区县
        List<String> wenzhouDistrict = new ArrayList<>();
        wenzhouDistrict.add("鹿城区");
        wenzhouDistrict.add("龙湾区");
        wenzhouDistrict.add("瓯海区");
        wenzhouDistrict.add("洞头区");
        wenzhouDistrict.add("永嘉县");
        zhejiangDistricts.add(wenzhouDistrict);
        // 嘉兴市下辖区县
        List<String> jiaxingDistrict = new ArrayList<>();
        jiaxingDistrict.add("南湖区");
        jiaxingDistrict.add("秀洲区");
        jiaxingDistrict.add("嘉善县");
        jiaxingDistrict.add("海盐县");
        jiaxingDistrict.add("海宁市");
        zhejiangDistricts.add(jiaxingDistrict);
        // 绍兴市下辖区县
        List<String> shaoxingDistrict = new ArrayList<>();
        shaoxingDistrict.add("越城区");
        shaoxingDistrict.add("柯桥区");
        shaoxingDistrict.add("上虞区");
        shaoxingDistrict.add("新昌县");
        shaoxingDistrict.add("诸暨市");
        zhejiangDistricts.add(shaoxingDistrict);

        // 山东省下辖区县
        List<List<String>> shandongDistricts = new ArrayList<>();
        // 济南市下辖区县
        List<String> jinanDistrict = new ArrayList<>();
        jinanDistrict.add("历下区");
        jinanDistrict.add("市中区");
        jinanDistrict.add("槐荫区");
        jinanDistrict.add("天桥区");
        jinanDistrict.add("历城区");
        shandongDistricts.add(jinanDistrict);
        // 青岛市下辖区县
        List<String> qingdaoDistrict = new ArrayList<>();
        qingdaoDistrict.add("市南区");
        qingdaoDistrict.add("市北区");
        qingdaoDistrict.add("黄岛区");
        qingdaoDistrict.add("崂山区");
        qingdaoDistrict.add("李沧区");
        shandongDistricts.add(qingdaoDistrict);
        // 烟台市下辖区县
        List<String> yantaiDistrict = new ArrayList<>();
        yantaiDistrict.add("芝罘区");
        yantaiDistrict.add("福山区");
        yantaiDistrict.add("牟平区");
        yantaiDistrict.add("莱山区");
        yantaiDistrict.add("蓬莱区");
        shandongDistricts.add(yantaiDistrict);
        // 潍坊市下辖区县
        List<String> weifangDistrict = new ArrayList<>();
        weifangDistrict.add("潍城区");
        weifangDistrict.add("寒亭区");
        weifangDistrict.add("坊子区");
        weifangDistrict.add("奎文区");
        weifangDistrict.add("临朐县");
        shandongDistricts.add(weifangDistrict);
        // 淄博市下辖区县
        List<String> ziboDistrict = new ArrayList<>();
        ziboDistrict.add("张店区");
        ziboDistrict.add("淄川区");
        ziboDistrict.add("博山区");
        ziboDistrict.add("临淄区");
        ziboDistrict.add("周村区");
        shandongDistricts.add(ziboDistrict);

        // 四川省下辖区县
        List<List<String>> sichuanDistricts = new ArrayList<>();
        // 成都市下辖区县
        List<String> chengduDistrict = new ArrayList<>();
        chengduDistrict.add("锦江区");
        chengduDistrict.add("青羊区");
        chengduDistrict.add("金牛区");
        chengduDistrict.add("武侯区");
        chengduDistrict.add("成华区");
        sichuanDistricts.add(chengduDistrict);
        // 绵阳市下辖区县
        List<String> mianyangDistrict = new ArrayList<>();
        mianyangDistrict.add("涪城区");
        mianyangDistrict.add("游仙区");
        mianyangDistrict.add("安州区");
        mianyangDistrict.add("三台县");
        mianyangDistrict.add("盐亭县");
        sichuanDistricts.add(mianyangDistrict);
        // 德阳市下辖区县
        List<String> deyangDistrict = new ArrayList<>();
        deyangDistrict.add("旌阳区");
        deyangDistrict.add("罗江区");
        deyangDistrict.add("中江县");
        deyangDistrict.add("广汉市");
        deyangDistrict.add("什邡市");
        sichuanDistricts.add(deyangDistrict);
        // 南充市下辖区县
        List<String> nanchongDistrict = new ArrayList<>();
        nanchongDistrict.add("顺庆区");
        nanchongDistrict.add("高坪区");
        nanchongDistrict.add("嘉陵区");
        nanchongDistrict.add("南部县");
        nanchongDistrict.add("营山县");
        sichuanDistricts.add(nanchongDistrict);
        // 宜宾市下辖区县
        List<String> yibinDistrict = new ArrayList<>();
        yibinDistrict.add("翠屏区");
        yibinDistrict.add("南溪区");
        yibinDistrict.add("叙州区");
        yibinDistrict.add("江安县");
        yibinDistrict.add("长宁县");
        sichuanDistricts.add(yibinDistrict);

        // 湖南省下辖区县
        List<List<String>> hunanDistricts = new ArrayList<>();
        // 长沙市下辖区县
        List<String> changshaDistrict = new ArrayList<>();
        changshaDistrict.add("芙蓉区");
        changshaDistrict.add("天心区");
        changshaDistrict.add("岳麓区");
        changshaDistrict.add("开福区");
        changshaDistrict.add("雨花区");
        hunanDistricts.add(changshaDistrict);
        // 株洲市下辖区县
        List<String> zhuzhouDistrict = new ArrayList<>();
        zhuzhouDistrict.add("荷塘区");
        zhuzhouDistrict.add("芦淞区");
        zhuzhouDistrict.add("石峰区");
        zhuzhouDistrict.add("天元区");
        zhuzhouDistrict.add("渌口区");
        hunanDistricts.add(zhuzhouDistrict);
        // 湘潭市下辖区县
        List<String> xiangtanDistrict = new ArrayList<>();
        xiangtanDistrict.add("雨湖区");
        xiangtanDistrict.add("岳塘区");
        xiangtanDistrict.add("湘潭县");
        xiangtanDistrict.add("湘乡市");
        xiangtanDistrict.add("韶山市");
        hunanDistricts.add(xiangtanDistrict);
        // 衡阳市下辖区县
        List<String> hengyangDistrict = new ArrayList<>();
        hengyangDistrict.add("珠晖区");
        hengyangDistrict.add("雁峰区");
        hengyangDistrict.add("石鼓区");
        hengyangDistrict.add("蒸湘区");
        hengyangDistrict.add("南岳区");
        hunanDistricts.add(hengyangDistrict);
        // 岳阳市下辖区县
        List<String> yueyangDistrict = new ArrayList<>();
        yueyangDistrict.add("岳阳楼区");
        yueyangDistrict.add("云溪区");
        yueyangDistrict.add("君山区");
        yueyangDistrict.add("岳阳县");
        yueyangDistrict.add("华容县");
        hunanDistricts.add(yueyangDistrict);

        // 湖北省下辖区县
        List<List<String>> hubeiDistricts = new ArrayList<>();
        // 武汉市下辖区县
        List<String> wuhanDistrict = new ArrayList<>();
        wuhanDistrict.add("江岸区");
        wuhanDistrict.add("江汉区");
        wuhanDistrict.add("硚口区");
        wuhanDistrict.add("汉阳区");
        wuhanDistrict.add("武昌区");
        hubeiDistricts.add(wuhanDistrict);
        // 黄石市下辖区县
        List<String> huangshiDistrict = new ArrayList<>();
        huangshiDistrict.add("黄石港区");
        huangshiDistrict.add("西塞山区");
        huangshiDistrict.add("下陆区");
        huangshiDistrict.add("铁山区");
        huangshiDistrict.add("阳新县");
        hubeiDistricts.add(huangshiDistrict);
        // 十堰市下辖区县
        List<String> shiyanDistrict = new ArrayList<>();
        shiyanDistrict.add("茅箭区");
        shiyanDistrict.add("张湾区");
        shiyanDistrict.add("郧阳区");
        shiyanDistrict.add("郧西县");
        shiyanDistrict.add("竹山县");
        hubeiDistricts.add(shiyanDistrict);
        // 宜昌市下辖区县
        List<String> yichangDistrict = new ArrayList<>();
        yichangDistrict.add("西陵区");
        yichangDistrict.add("伍家岗区");
        yichangDistrict.add("点军区");
        yichangDistrict.add("猇亭区");
        yichangDistrict.add("夷陵区");
        hubeiDistricts.add(yichangDistrict);
        // 襄阳市下辖区县
        List<String> xiangyangDistrict = new ArrayList<>();
        xiangyangDistrict.add("襄城区");
        xiangyangDistrict.add("樊城区");
        xiangyangDistrict.add("襄州区");
        xiangyangDistrict.add("南漳县");
        xiangyangDistrict.add("谷城县");
        hubeiDistricts.add(xiangyangDistrict);

        // 河南省下辖区县
        List<List<String>> henanDistricts = new ArrayList<>();
        // 郑州市下辖区县
        List<String> zhengzhouDistrict = new ArrayList<>();
        zhengzhouDistrict.add("中原区");
        zhengzhouDistrict.add("二七区");
        zhengzhouDistrict.add("管城回族区");
        zhengzhouDistrict.add("金水区");
        zhengzhouDistrict.add("上街区");
        henanDistricts.add(zhengzhouDistrict);
        // 开封市下辖区县
        List<String> kaifengDistrict = new ArrayList<>();
        kaifengDistrict.add("龙亭区");
        kaifengDistrict.add("顺河回族区");
        kaifengDistrict.add("鼓楼区");
        kaifengDistrict.add("禹王台区");
        kaifengDistrict.add("祥符区");
        henanDistricts.add(kaifengDistrict);

        districts.add(beijingDistricts);
        districts.add(shanghaiDistricts);
        districts.add(guangdongDistricts);
        districts.add(jiangsuDistricts);
        districts.add(zhejiangDistricts);
        districts.add(shandongDistricts);
        districts.add(sichuanDistricts);
        districts.add(hunanDistricts);
        districts.add(hubeiDistricts);
        districts.add(henanDistricts);
    }

    private void loadRegionDialog() {
        regionDialog = new BottomSheetDialog(this);
        regionDialog.setContentView(R.layout.dialog_region_selector);
        spProvince = regionDialog.findViewById(R.id.sp_province);
        spCity = regionDialog.findViewById(R.id.sp_city);
        spDistrict = regionDialog.findViewById(R.id.sp_district);
        btnConfirmRegion = regionDialog.findViewById(R.id.btn_confirm);
        // 设置省份选择器
        ArrayAdapter<String> provinceAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, provinces);
        provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spProvince.setAdapter(provinceAdapter);
        spProvince.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedProvince = provinces.get(position);
                List<String> cityList = cities.get(position);
                // 设置城市选择器
                ArrayAdapter<String> cityAdapter = new ArrayAdapter<>(BianjidizhiActivity.this,
                        android.R.layout.simple_spinner_item, cityList);
                cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spCity.setAdapter(cityAdapter);
                spCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int cityPosition, long id) {
                        selectedCity = cityList.get(cityPosition);
                        List<String> districtList = districts.get(position).get(cityPosition);
                        // 设置区县选择器
                        ArrayAdapter<String> districtAdapter = new ArrayAdapter<>(BianjidizhiActivity.this,
                                android.R.layout.simple_spinner_item, districtList);
                        districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spDistrict.setAdapter(districtAdapter);
                        spDistrict.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int districtPosition, long id) {
                                selectedDistrict = districtList.get(districtPosition);
                            }
                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {}
                        });
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {}
                });
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });
        btnConfirmRegion.setOnClickListener(v -> {
            tvRegion.setText(selectedProvince + " " + selectedCity + " " + selectedDistrict);
            regionDialog.dismiss();
        });
    }
    private void saveAddress() {
        String receiver = etReceiver.getText().toString().trim();
        String phone = etPhone.getText().toString().trim();
        String region = tvRegion.getText().toString().trim();
        String detailAddress = etDetailAddress.getText().toString().trim();
        boolean isDefault = switchDefault.isChecked();
        if (receiver.isEmpty() || phone.isEmpty() || region.equals("请选择地区") || detailAddress.isEmpty()) {
            Toast.makeText(this, "请完整填写地址信息", Toast.LENGTH_SHORT).show();
            return;
        }
        // 更新地址对象（保留原ID）
        editAddress.receiver = receiver;
        editAddress.phone = phone;
        editAddress.region = region;
        editAddress.detailAddress = detailAddress;
        editAddress.setDefault(isDefault);
        // 保存到SharedPreferences
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        String addressId = editAddress.getId();
        editor.putString(addressId + "_receiver", receiver);
        editor.putString(addressId + "_phone", phone);
        editor.putString(addressId + "_region", region);
        editor.putString(addressId + "_detailAddress", detailAddress);
        editor.putBoolean(addressId + "_isDefault", isDefault);
        // 处理默认地址
        if (isDefault) {
            for (String key : prefs.getAll().keySet()) {
                if (key.endsWith("_isDefault") && !key.startsWith(addressId)) {
                    editor.putBoolean(key, false);
                }
            }
        }
        editor.apply();
        Log.d("BianjidizhiActivity", "保存地址: " + receiver + ", ID: " + addressId);
        // 发送地址更新广播
        sendAddressUpdatedBroadcast();
        // 返回结果
        Intent intent = new Intent();
        intent.putExtra("address", editAddress);
        intent.putExtra("isDelete", false);
        setResult(RESULT_OK, intent);
        finish();
    }
    private void deleteAddress() {
        if (editAddress == null || editAddress.getId() == null) {
            Toast.makeText(this, "地址信息为空，无法删除", Toast.LENGTH_SHORT).show();
            return;
        }
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        String addressId = editAddress.getId();
        editor.remove(addressId + "_receiver");
        editor.remove(addressId + "_phone");
        editor.remove(addressId + "_region");
        editor.remove(addressId + "_detailAddress");
        editor.remove(addressId + "_isDefault");
        editor.apply();
        Log.d("BianjidizhiActivity", "删除地址: " + editAddress.getReceiver() + ", ID: " + addressId);
        Intent intent = new Intent();
        intent.putExtra("address", editAddress);
        intent.putExtra("isDelete", true);
        setResult(RESULT_OK, intent);
        finish();
    }
    // 发送地址更新广播
    private void sendAddressUpdatedBroadcast() {
        Intent broadcastIntent = new Intent("ACTION_ADDRESS_UPDATED");
        sendBroadcast(broadcastIntent);
    }
}